const {http,add,getMusicUrl} = require("../../utils/util");

// pages/music/music.js
Page({
  data: {
    value:"",
    music:[],
    prevId:""
  },

  handlePlay(event){
    var {aid} = event.currentTarget.dataset;
    var music = this.data.music;
    music.forEach(item=>{
      item.isPlay = aid == item.id?!item.isPlay:false;
    })
    this.setData({music,prevId:aid});
  },

  onSubmit(event){
    var keyword = event.detail.value.name;
    getMusicUrl(keyword).then(res=>{
      var music = [];
      this.handleHttpData(res,music);
      this.setData({music,value:""});
    })
  },
  handleHttpData(res,arr){
    var songs = res.data.result.songs;
    songs.forEach(item=>{
      var {id,name,artists,duration} = item;
      var imgUrl = artists.map(item=>{
        return item.img1v1Url;
      });
      duration = Math.ceil(duration/1000);
      var minute = Math.ceil(duration/60);
      minute = add(minute);
      var second = add(duration % 60);
      duration = minute + ":" + second;
      var artistsName = artists.map(item=>{
        return item.name;
      }).join("/");
      arr.push({id,name,artistsName,duration,imgUrl});
      arr.isPlay = true;
    })
    return arr;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  // wx.showLoading({
  //   title: '数据加载中',
  // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})